package com.gxc.controller;

import com.gxc.entity.Dept;
import com.gxc.service.DeptService;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author GongXincheng
 * @since 2019-08-19 21:39
 */
@RestController
public class DeptController {

    @Resource
    private DeptService deptService;

    @Resource
    private DiscoveryClient discoveryClient;

    @GetMapping("/test")
    public String hello() {
        return "Hello World !";
    }

    @GetMapping("/dept/{id}")
    public Dept findById(@PathVariable Long id) {
        return deptService.find(id);
    }

    @PostMapping("/dept")
    public Boolean add(@RequestBody Dept dept) {
        return deptService.add(dept);
    }

    @GetMapping("/dept/list")
    public List<Dept> list() {
        return deptService.list();
    }

    @GetMapping("/discovery")
    public Object discovery() {
        List<String> services = discoveryClient.getServices();
        services.forEach(System.out::println);

        List<ServiceInstance> instances = discoveryClient.getInstances("SPRINGCLOUD-DEPT-PROVIDER-8001");
        instances.forEach(e -> {
            System.out.println("InstanceId：" + e.getServiceId() + "，Host：" + e.getHost()
                    + "，Port：" + e.getPort() + "，URI：" + e.getUri());
        });
        return this.discoveryClient;
    }

}
